function [K,Weights,name]=puttestshere3(F,width,height,numparams,corrs,showplots)
%close all
if(nargin<6)
    showplots=0;
end
Weights=[];
name='sum curves';
K=[width 0 width/2; 0 width height/2; 0 0 1];
numsl=100;

usedFs=[];
for i=1:size(F,2)
    if(size(F{1,i},1)~=0)
        usedFs=[usedFs i];
    end
end
origusedF=usedFs;
warning('off','signal:findpeaks:noPeaks');
k=0;

k=k+1;params(k) = struct('name', 'focal','ymeasure','foc1' ,'xmeasure','foc2', 'yKrow', 1, 'yKcol', 1, 'xKrow',2, 'xKcol', 2);
%k=k+1;params(k) = struct('name', 'oc','ymeasure','xc' ,'xmeasure','yc', 'yKrow', 1, 'yKcol', 3, 'xKrow',2, 'xKcol', 3);
%k=k+1;params(k) = struct('name', 'focal','ymeasure','foc2' ,'xmeasure','foc1', 'yKrow', 2, 'yKcol', 2, 'xKrow',1, 'xKcol', 1);
k=k+1;params(k) = struct('name', 'oc','ymeasure','yc' ,'xmeasure','xc', 'yKrow', 2, 'yKcol', 3, 'xKrow',1, 'xKcol', 3);


rep=1;

for p=1:rep

for p=1:size(params,2)
    newusedFs=[];
    usedFs=origusedF;
    ci=1;
    
    datapts=cell(size(usedFs,2),1);
    
  
    
    for i=1:size(usedFs,2)
       [sols,xs]= provideFamilySolution_matrixnorm(F{1,usedFs(1,i)},width,height,2,numsl,K,params(p).ymeasure);
       %   [sols,xs]= provideFamilySolution_matrixnormPOLYFMIN(F{1,usedFs(1,i)},width,height,2,numsl,K,params(p).ymeasure);

        
        if(size(sols,1)>0)
            datapts{ci,1}(:,2)=sols;
            datapts{ci,1}(:,1)=xs;
            datapts{ci,1}(:,3)=ones(numsl,1)*usedFs(1,i);
            datapts{ci,1}(:,4)=zeros(numsl,1);
            newusedFs=[newusedFs usedFs(1,i)];
            ci=ci+1;
        else 
          %  display([num2str(i) ' was a bad f matrix ']);
        end
        
    end
           

    if(size(newusedFs,2)<2)
        display([ ' out of ' num2str(size(usedFs,2)) ' got ' num2str(size(newusedFs,2)) ' non-degenerate for ' params(p).name ]);
        display('degenerate');
        return
    end
    datapts = datapts(~cellfun(@isempty, datapts));
    usedFs= newusedFs;
    
    
  
     [x, bestf,bestindex,datapts ]=finduniquesolution(datapts,usedFs,width, height,params(p).name);
     %    [x, bestf,bestindex,datapts ]=finduniquesolutionnonlin(datapts,usedFs,width, height,params(p).name);

    
    K(params(p).xKrow,params(p).xKcol)=x(1,1);
    K(params(p).yKrow,params(p).yKcol)=x(2,1);
    
    if(showplots==1)
        plotcurvegraphs(datapts,p,params,bestf, bestindex);
    end
    
    
end

end

end

function [x, bestf,bestindex,newdtp ]=finduniquesolution(datapts,usedFs,width, height,type)
minans=100;
  maxd=-100000000;
    bestf=1;
    bestindex=1;
  for i=1:size(usedFs,2)
        for j=1:size(usedFs,2)
            if(j~=i)
                 incvec=exp((-abs(datapts{i,1}(:,2)-datapts{j,1}(:,2)))/(width/10));
                 incvec(datapts{i,1}(:,2)<minans,1)=0;
                 incvec(datapts{j,1}(:,2)<minans,1)=0;
                datapts{i,1}(:,4)=datapts{i,1}(:,4)+ incvec;
            end
        end
        
      
        [pks,locs] = findpeaks(datapts{i,1}(:,4));
    
            
        for q=1:size(pks,2)
            
            
            if(pks(1,q)>maxd && datapts{i,1}(locs(1,q),2)>minans )
                maxd=pks(1,q);
                bestf=i;
                bestindex=locs(1,q);
            end
        end
        
  end
    newdtp=datapts;
  x=[datapts{bestf,1}(bestindex,1) ; datapts{bestf,1}(bestindex,2) ];

end

function [x, bestf,bestindex,newdtp ]=finduniquesolutionnonlin(datapts,usedFs,width, height,type)

x0=[0,0];
bestf=1;
bestindex=1;
newdtp=datapts;

if(strcmp(type,'focal')==1)
    x0=[width ; width];
    scaler=width/5;
else
    x0=[width/2 ; height/2];
    scaler=width/8;
end
  f = @(x)geometricnonlinselfcal(x,datapts,usedFs,width, height,scaler);
[x,fval,exitflag,output] = fminsearch(f,x0);

end


function []=plotcurvegraphs(datapts,p,params,index,bestindex)
numfs=size(  datapts,1);
cVec = 'bgrcmykbgrcmykbgrcmykbgrcmyk';%, cVec = [cVec cVec];

xval=datapts{index,1}(bestindex,1);
yval=datapts{index,1}(bestindex,2);

label1= params(p).ymeasure;
label2= params(p).xmeasure;

figure, hold on
for i=1:numfs
    plot(datapts{i,1}(:,1),datapts{i,1}(:,2),cVec(1+mod(i,length(cVec))))
end
xlabel(label2);
ylabel(label1);
title(['clusters '  label1 ' versus '  label2]);
plot(xval,yval, 'marker','o','markersize',10,'markeredgecolor',[1,0,0],'markerfacecolor',[1,0,0]);
hold off

figure, hold on
for i=1:numfs
    plot(datapts{i,1}(:,1),datapts{i,1}(:,4),cVec(1+mod(i,length(cVec))))
end
d=ylim;
line([xval xval], [0 d(2)],'Color','r','LineStyle',':');
xlabel(label2);
ylabel(label1);
title(['sum curves '  label1 ' versus '  label2]);
hold off

figure
hold on
plot(datapts{index,1}(:,1),(datapts{index,1}(:,4)/mean(datapts{index,1}(:,4)))*mean(datapts{index,1}(:,2)),'r',datapts{index,1}(:,1),datapts{index,1}(:,2),'b')
d=ylim;
f=xlim;
plot(xval,yval, 'marker','o','markersize',10,'markeredgecolor',[1,0,0],'markerfacecolor',[1,0,0]);
line([xval xval], [0 d(2)],'Color','r','LineStyle',':');
line([0 f(2)], [yval yval],'Color','b','LineStyle',':');

legend('sum curve','solution family');


xlabel(label2);
ylabel(label1);
title([' wining '  label1 ' versus '  label2 ' winning x at index ' num2str(index) ' -> ( ' num2str(datapts{index,1}(bestindex,1)) ' , '   num2str(datapts{index,1}(bestindex,2)) ' )' ]);
hold off
end
